--[[
 -- *名称:SuitAnalysisFactory
 -- *描述:工厂类
 -- *版权:Copyright © 2016-2018 深圳市三只小熊科技有限公司 版权所有
 -- *作者:Administrator
 -- *创建日期:2017-11-08
 -- *修改日期:2017-11-08
 ]]
local SuitAnalysisFactory = pokercore.class();
---
 -- [ctor 默认类的构造方法]
 -- @DateTime 2017-09-20
 -- @param     [description]
 -- @return    [description]
 --
function SuitAnalysisFactory:ctor()
   --牌型规则集合（从大到小排列）
   self.suitAnalyzers = {} ;
   --print("实例化分析工厂");
end

---
 -- [dtor 默认类的销毁方法]
 -- @DateTime 2017-09-20
 -- @param     [description]
 -- @return    [description]
 --
function SuitAnalysisFactory:dtor()
  
end

---
 -- [getSuitAnalyzer 获取对应的分析器]
 -- @DateTime 2017-11-08
 -- @param    type [description]
 -- @return    [description]
 --
function SuitAnalysisFactory:getSuitAnalyzer(type)
    for _,analyzer in pairs(self.suitAnalyzers) do
    	--print("suitType",analyzer:getSuitType());
    	--print("Type",type);
    	if analyzer:getSuitType():equals(type) then
    		return analyzer ;
    	end
    end
end

---
 -- [addSuitAnalyzer 添加一个新的分析器]
 -- @DateTime 2017-11-08
 -- @param    analyzer [description]
 --
function SuitAnalysisFactory:addSuitAnalyzer(analyzer)
	--print("添加一个新的分析器 analyzer type- >".. analyzer:getSuitType():getType())
	table.insert(self.suitAnalyzers,analyzer);
	-- table.sort(self.suitAnalyzers,function(o1,o2)
 --       local result = -(o1:getPriority() - o2:getPriority()) ;
 --       if result >= 0 then
 --       	return true ;
 --       else
 --       	return false ;
 --       end;
	-- end)
end

---
 -- [listAbstractSuitType 列出所有检测type]
 -- @DateTime 2017-11-08
 -- @param    desc [description]
 -- @return    [description]
 --
function SuitAnalysisFactory:listAbstractSuitType(desc)
	local typeList = {} ;
	for _,abstractSuitAnalyzer in pairs(self.suitAnalyzers) do
		if abstractSuitAnalyzer:getSuitType() then
			table.insert(typeList,abstractSuitAnalyzer:getSuitType())
		end
	end
	----dump(typeList,"####SuitAnalysisFactory:listAbstractSuitType typeList");
	table.sort(typeList,function(a,b)
	   local result = a:compareTo(b) ;
	   if desc then	   
		   	if result>=0 then
		   		return false ;
		   	else
		   		return true  ;
		   	end
	   else
	    	if result>=0 then
		   		return true ;
		   	else
		   		return false  ;
		   	end
	   end
	end)
	return typeList ;
   ----dump(typeList,"####SuitAnalysisFactory:listAbstractSuitType typeList");
end

---
 -- [listPossibleTypes 列出所有可能的type]
 -- @DateTime 2017-11-08
 -- @param    cards [description]
 -- @param    term [description]
 -- @param    type [description]
 -- @param    desc [description]
 -- @return    [description]
 --
function SuitAnalysisFactory:listPossibleTypes(cards,term,type,desc)
	local typeList = {} ;
	for i = 1 , #self.suitAnalyzers do
		local suitAnalyzer = self.suitAnalyzers[i] ;
		if not type then
			if suitAnalyzer:match(cards,term) then
				--print("SuitAnalysisFactory:listPossibleTypes insert1 type",suitAnalyzer:getSuitType());
				table.insert(typeList,suitAnalyzer:getSuitType());
			end
		else
			--要求大于suitAnalyzer.getSuitType() 大于或等于 type 这里不能用 <= 0 作判定 
			if type:compareTo(suitAnalyzer:getSuitType()) < 0
				or (type == suitAnalyzer:getSuitType()) then
				if suitAnalyzer:match(cards,term) then
					--print("SuitAnalysisFactory:listPossibleTypes insert2 type",suitAnalyzer:getSuitType());
					table.insert(typeList,suitAnalyzer.getSuitType())
				end
			end
		end
	end
	table.sort(typeList,function(a,b)
	    local result = a:compareTo(b) ;
	    if desc then	   
			return result>=0 and false or true;
	    else
	   		return result>=0 and true or false ;
	    end
	end)
	return typeList ;
end

---
 -- [analysis 获取分析结果]
 -- @DateTime 2017-11-08
 -- @param    cards [description]
 -- @param    term [description]
 -- @param    type [description]
 -- @return    [description]
 --
function SuitAnalysisFactory:analysis(cards,term,type)
	if cards and type then
		local analyzer = self:getSuitAnalyzer(type);
		if analyzer and analyzer:match(cards,term) then
			analyzer:analysize(cards,term) ;
		end
	end
	return cards:getResult();
end


function SuitAnalysisFactory:analysisLaizi(cards,term,type)
	local result = {} ;
	if cards and type then
		--print("SuitAnalysisFactory:analysisLaizi");
		--dump(type,"type->")
		--dump(cards,"cards->")
		local analyzer = self:getSuitAnalyzer(type) ;
		if analyzer and analyzer:match(cards, term) then
			table.addAll(result,analyzer:analysizeLaizi(cards, term, false));
		end
	end
	return result ;
end

---
 -- [listInTypePossibleCards 提牌算法]
 -- @DateTime 2017-11-10
 -- @param    cards [description]
 -- @param    term [description]
 -- @param    type [description]
 -- @return    [description]
 --
function SuitAnalysisFactory:listInTypePossibleCards( cards, term, type)
	local result = {} ;
	if cards and type then
		-- print("SuitAnalysisFactory:listInTypePossibleCards");
		local analyzer = self:getSuitAnalyzer(type);
		if analyzer then
			-- print("SuitAnalysisFactory:listInTypePossibleCards 2 ");
			table.addAll(result,analyzer:listPossibleCards(cards,term,false));
		end
		-- print("SuitAnalysisFactory:listInTypePossibleCards 3");
	end
	return result ;
end


return SuitAnalysisFactory;

